/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page;

import com.rafkind.resrev.dao.ResourceDao;
import com.rafkind.resrev.dao.UserDao;
import com.rafkind.resrev.data.Color;
import com.rafkind.resrev.data.Condition;
import com.rafkind.resrev.data.Resource;
import com.rafkind.resrev.data.User;
import com.rafkind.resrev.web.Authorized;
import com.rafkind.resrev.web.Session;
import com.rafkind.resrev.web.page.component.TitleMenu;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
@Authorized
public class EditResource extends WebPage {
  
  @SpringBean
  private ResourceDao resourceDao;
  
  @SpringBean
  private UserDao userDao;
      
  public EditResource(IModel<Resource> model, final Page previous) {
    super(model);
    
    add(new TitleMenu("titleMenu"));
    
    Form editResourceForm = new Form("editResourceForm"){

      @Override
      protected void onSubmit() {
        Resource resource = (Resource) EditResource.this.getDefaultModelObject();
        
        resourceDao.saveResource(resource);
        
        setResponsePage(previous);
      }    
    };
    
    editResourceForm.add(new FeedbackPanel("feedback"));
    
    editResourceForm.add(new FormComponentFeedbackBorder("nameBorder").add(
        new RequiredTextField("name", 
        new PropertyModel(model.getObject(), "name")).setLabel(new Model("Name"))));
    
    editResourceForm.add(
        new TextArea("description", 
        new PropertyModel(model.getObject(), "description")));
    
    editResourceForm.add(
        new DropDownChoice("color", 
        new PropertyModel(model.getObject(), "color"),
        Arrays.asList(Color.values())));
    
    editResourceForm.add(
        new DropDownChoice("condition", 
        new PropertyModel(model.getObject(), "currentCondition"),
        Arrays.asList(Condition.values())));
    
    editResourceForm.add(
        new DropDownChoice("authorizor", 
          new AuthorizorModel((Resource) model.getObject()),
          new UserChoicesModel(userDao),
          new UserDropdownChoiceRenderer()){
      @Override
      public boolean isEnabled() {
        return ((Session)getSession()).getCurrentUser().isAdmin();
      }
    }.setNullValid(false));
    
    editResourceForm.add(
        new Button("saveButton", new PropertyModel(this, "saveButtonText")));
    
    editResourceForm.add(
        new Button("cancelButton", new PropertyModel(this, "cancelButtonText")){
      @Override
      public void onSubmit() {
        setResponsePage(previous);
      }
    }.setDefaultFormProcessing(false));
    
    add(editResourceForm);
  }
  
  public String getSaveButtonText() {
    Resource r = (Resource) getDefaultModelObject();
    
    if (r.isNew())
      return "Create new resource";
    else
      return "Update resource";
  }
  
  public String getCancelButtonText() {
    Resource r = (Resource) getDefaultModelObject();
    
    if (r.isNew())
      return "Cancel resource creation";
    else
      return "Cancel resource update";
  }
  
  public static class AuthorizorModel extends Model<User> {
    
    private Resource resource;
    private User dummyUser;
    
    public AuthorizorModel(Resource resource) {
      super(resource.getAuthorizor());
      this.resource = resource;
      dummyUser = User.createSentinelUser();
    }
    
    @Override
    public User getObject() {
      if (resource == null || resource.getAuthorizor() == null)
        return dummyUser;
      else
        return resource.getAuthorizor();
    }

    @Override
    public void setObject(User newAuthorizor) {      
      if (resource != null) {        
        if (newAuthorizor == null || newAuthorizor.equals(dummyUser))
          resource.setAuthorizor(null);
        else
          resource.setAuthorizor(newAuthorizor);
      }
    }        
  }
  
  public static class UserChoicesModel 
      extends LoadableDetachableModel<List<User>> {
    
    private UserDao userDao;

    public UserChoicesModel(UserDao userDao) {
      this.userDao = userDao;
    }
    
    @Override
    protected List<User> load() {
      List<User> users = (List<User>) userDao.getAllUsers();
      users.add(0, User.createSentinelUser());
      return users;
    }  
  }
  
  public static class UserDropdownChoiceRenderer 
      implements IChoiceRenderer<User> {

    public Object getDisplayValue(User user) {
      return user.getName();
    }

    public String getIdValue(User user, int arg1) {
      return user.getId().toString();      
    }   
  }
}
